angular.module('memydatacontrollers', [])

  .controller('MeMyDataCtrl', function ($scope,$ionicActionSheet, $state, $timeout, Storage,Loading,Toast, MeMyData, ENV) {

    var memberId;
    $scope.ENV = ENV;
    $scope.isHasIdentify = false;
    $scope.identityPass = false;
    $scope.identityContinue = false;
    $scope.identityFailed = false;
    $scope.isIdentity = true;
    $scope.reIdentity = false;

    var identifyGrade = 1000;


    $scope.myData = {
      member_id: 0,
      member_phone: "",
      member_name: "",
      member_gender: "",
      member_birthday: "",
      member_job: "",
      member_hobby: "",
      member_contact: "",
      member_email: "",
      member_address: "",
      member_img_url: "",
      member_identify_state: "",
      member_state: "",
      mr_id: 0,
      mr_name: "",
      mr_img_url: "",
      member_point_count_history: 0
    };

    //进入该界面时，加载会员资料数据
    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      MeMyData.getMeInfo(memberId);
    });

    //获取返回的json对象，赋值给页面
    $scope.$on('Me.meInfo', function () {
      var obj = MeMyData.getMeInfoJson();
      $scope.myData.member_phone = obj.member_phone;
      $scope.myData.member_name = obj.member_name;
      $scope.myData.member_gender = obj.member_gender;
      $scope.myData.member_birthday = obj.member_birthday;
      $scope.myData.member_job = obj.member_job;
      $scope.myData.member_hobby = obj.member_hobby;
      $scope.myData.member_contact = obj.member_contact;
      $scope.myData.member_email = obj.member_email;
      $scope.myData.member_address = obj.member_address;
      $scope.myData.member_img_url = obj.member_img_url;
      $scope.myData.member_identify_state = obj.member_identify_state;
      $scope.myData.member_state = obj.member_state;
      $scope.myData.mr_id = obj.mr_id;
      $scope.myData.mr_name = obj.mr_name;
      $scope.myData.mr_img_url = obj.mr_img_url;
	 identifyGrade = obj.identifyPoint;
      //判断认证情况
      if (obj.member_identify_state != "未审核") {
        $scope.isHasIdentify = true;
        if (obj.member_identify_state == "正在审核") {
          $scope.identityContinue = true;
          $scope.identityPass = false;
          $scope.identityFailed = false;
          $scope.isIdentity = true;
        } else if (obj.member_identify_state == "审核通过") {
          $scope.identityContinue = false;
          $scope.identityPass = true;
          $scope.identityFailed = false;
          $scope.isIdentity = true;
        } else if (obj.member_identify_state == "审核失败") {
          $scope.identityContinue = false;
          $scope.identityPass = false;
          $scope.identityFailed = true;
          $scope.isIdentity = true;
        }
      } else {
        if (obj.member_point_count_history > identifyGrade) {
          $scope.isIdentity = false;
        }
      }

    });

    //发送认证消息给后台
    $scope.sendIdentifyMsg = function () {
      MeMyData.sendIdentifyMsgById(memberId);
    };

    //加载接收到的广播数据
    $scope.$on('Me.mydataIdentifyGot', function () {
      var obj = MeMyData.getIdentityMsgJson();
      Toast.showMessage(obj.identityMsg);
      $scope.identityContinue = true;
      $scope.identityPass = false;
      $scope.identityFailed = false;
      $scope.isIdentity = true;
    });

    //发送表单数据给后台，保存个人信息的修改
    $scope.saveMeInfo = function () {
      if ($scope.myData.member_name == '') {
        Toast.showMessage("昵称不能为空");
      } else if ($scope.myData.member_gender == '') {
        Toast.showMessage("性别不能为空");
      } else if ($scope.myData.member_img_url == '') {
        Toast.showMessage("头像不能为空");
      } else {
        //提交表单
        $scope.networkOk = false;
        MeMyData.saveMemberInfo(memberId, $scope.myData.member_name, $scope.myData.member_gender, $scope.myData.member_birthday, $scope.myData.member_hobby, $scope.myData.member_contact, $scope.myData.member_email, $scope.myData.member_address, $scope.myData.member_job, $scope.myData.member_img_url);
        Loading.showLoading();
        $timeout(function () {
          Loading.hideLoading();
          if (!$scope.networkOk) {
            Toast.showMessage("网络异常，请稍后再试....");
          }
        }, 5000);
      }
    };

    //加载接收到的广播数据
    $scope.$on('Me.memberInfoUpdated', function () {
      var obj = MeMyData.getInfoUpdateJson();
      $scope.networkOk = true;
      Loading.hideLoading();
      Toast.showMessage(obj.umInfoMsg);
    });

    //显示选项框，拍照 或 从相册提取
    $scope.showSelectOption = function () {
      console.log($scope.myData);

      var actionSheet = $ionicActionSheet.show({
        buttons: [
          {text: '拍照'},
          {text: '从相册选择'}
        ],
        titleText: '修改头像',
        cancelText: '取消',
        buttonClicked: function (index, memberId) {
          //点击按钮触发对应的事件
          MeMyData.takePictureByIndex(index);
        }
      });
      //两秒后自动关闭
      $timeout(function () {
        actionSheet();
      }, 2000);
    };

    //更新界面的头像
    $scope.$on('Me.uploadAvatar', function () {
      //接收到头像，路径
      $scope.$apply(function () {
        $scope.myData.member_img_url = MeMyData.getUploadAvatarJson();
      });


    });

    //显示选项框，选择性别
    $scope.showGenderSelectOption = function () {
      var hideSheet = $ionicActionSheet.show({
        buttons: [
          {text: '男'},
          {text: '女'}
        ],
        cancelText: '取消',
        cancel: function () {

        },
        buttonClicked: function (index) {
          if (index == 0) {
            //将界面的性别数据改为男
            $scope.myData.member_gender = "男";
          } else if (index == 1) {
            $scope.myData.member_gender = "女";
          }
          return true;
        }
      });
      //两秒后自动关闭
      $timeout(function () {
        hideSheet();
      }, 2000);
    };

    //跳转到修改密码界面
    $scope.gotoModifyPwd = function () {
      $state.go('revise-password');
    };


    //跳转到个人等级界面
    $scope.gotoMyPointPage = function () {
      $state.go('my-level');
    };



    //选择出生日期的时间
    //Android
    $('#bdTime').click(function(){
      $(this).mydate({format:"yyyy,MM,dd"},function(date){
        $scope.$apply(function () {
          $scope.myData.member_birthday = formatDate(date);
        });
      },function(){});
    });
    //IOS
   /* $scope.selectBirthdayTime = function () {
      var options = {
        date: new Date(),
        mode: 'date',
        doneButtonLabel: '确定',
        doneButtonColor: '#000000',
        cancelButtonLabel: '取消',
        cancelButtonColor: '#000000'
      };
      datePicker.show(options, function (date) {
        $scope.$apply(function () {
          $scope.myData.member_birthday = formatDate(date);
        });
      });
    }*/


  });
